home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 001a / combi11c.zip / CMBTEST.C next >
C/C++ Source or Header  |  1992-08-09  |  8KB  |  202 lines

  1. /*        COMBI-Disk demonstration/test program
  2.  *        by Vadim V. Vlasov, Moscow, 1991, 1992.
  3.  *
  4.  * This program may be used to test the reliability of COMBI-disk strategy of
  5.  * memory reallocation between RAM disk and disk cache.
  6.  * It also may demonstrate/test reliability of write caching.
  7.  * 
  8.  * You may modify this program at Your wish, however I'm not responsible
  9.  * for changes You make.
  10.  * 
  11.  * The idea is the following: this program creates 14 files in a directory
  12.  * You specify and randomly writes, seeks, deletes some of the files or
  13.  * performes some DOS command (I've chosen CHKDSK since it reads much from
  14.  * (hard) disk another useful alternative would be some XMS test program).
  15.  * On its exit it leaves some of the files (maybe not all) which contain
  16.  * text: "This text is written to file #<file number>, series <number>..."
  17.  * where <file number> should coincide with the number in the file name,
  18.  * number of lines in each series may be different and moreover the texts
  19.  * may appear not ordered due to random seek. Since pseudo-random number 
  20.  * generator is used the resulting files should be the same regardless of 
  21.  * the target directory provided other command line parameters are the same.
  22.  * So You may run it first on a hard disk and then on COMBI's RAM disk
  23.  * (or vice versa) and compare all the resulting files. If the files are
  24.  * the same there is no error.
  25.  * 
  26.  * Usage:
  27.  *
  28.  * cmbtest [<path> [/c<cycles>] [/r<repetitions>] [/s <command>]
  29.  *
  30.  * where "path"        is a drive letter with ':' or a path ending with '\';
  31.  *       "cycles"      is number of cycles (default - 200); this affects the
  32.  *                     time it takes to run the program and contents of files
  33.  *                     but not necessarily increases file sizes;
  34.  *       "repetitions" is maximum number of lines per write (default - 60),
  35.  *                     it is also used for initialization of random number
  36.  *                     generator; as a rule increases file sizes (and disk 
  37.  *                     storage required);
  38.  *       "command"     is a DOS command to run (default - chkdsk)
  39.  *                     (if command consists of few words it should be
  40.  *                     written in double quotes e.g. "dir *.exe /s").
  41.  *
  42.  * The source is written for MS C 6.0 and may be compiled in TINY or
  43.  * SMALL memory model.
  44.  * 
  45.  */
  46.  
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include <stdlib.h>
  50. #include <io.h>
  51. #include <fcntl.h>
  52. #include <sys/types.h>
  53. #include <sys/stat.h>
  54. #include <errno.h>
  55.  
  56. #define  rep_dflt 60
  57. #define  cycles_dflt   200
  58. #define  NUMFILES 14
  59.  
  60.  
  61. main(int argc, char *argv[])
  62. {
  63.     int     fhs[NUMFILES]={0};        /* for file handles */
  64.     int     i, j, rn, fn, len;        /* miscellaneous variables */
  65.     int     cycles=cycles_dflt;       /* number of cycles */
  66.     int     repetition=rep_dflt;      /* max number of repetitions  */
  67.     char    name[128];                /* this stores name of file we open */
  68.     char   *command = "CHKDSK";       /* CHKDSK makes a lot of job for
  69.                                        * hard disk and hence for cache  */
  70.     long    nowlen[NUMFILES], nowpos[NUMFILES], seek_shift;
  71.     char    dirname[128] = "";        /* target directory name      */
  72.     char    text1[128];
  73.     char   *form1="This text is written to file #%d, series %d...\n";
  74.                                       /* format of output text  */
  75.  
  76.     if(argc > 1)          /* if there is an argument it is directory name */
  77.         strcpy(dirname,argv[1]);
  78.     i = strlen(dirname);
  79.     if( i && dirname[i-1] != '\\' && dirname[i-1] != ':') {
  80.       dirname[i] = '\\';    /* add backslash if necessary */
  81.       dirname[i+1] = '\0';
  82.     }
  83.  
  84.     if(argc > 2) {
  85.       for( i = 2; i < argc; i++) {
  86.         if(argv[i][0] == '/') {
  87.           switch(argv[i][1]){
  88.             case 'c':
  89.             case 'C':
  90.               cycles = atoi(argv[i]+2);
  91.               if ( (cycles <= 0) || (cycles > 1000))
  92.                 cycles = cycles_dflt;
  93.               printf("Cycles = %d\n",cycles);
  94.               break;
  95.             case 'r':
  96.             case 'R':
  97.               repetition = atoi(argv[i]+2);
  98.               if ( (repetition <= 0) || (repetition > 1000))
  99.                 repetition = rep_dflt;
  100.               printf("Repetitions = %d\n", repetition);
  101.               break;
  102.             case 's':
  103.             case 'S':
  104.               command = argv[i+1];
  105.               i++;
  106.               break;
  107.           }
  108.         }
  109.       }
  110.     }
  111.  
  112.     srand(repetition + 11);   /* init (pseudo)random number generator */
  113.  
  114.     for(i=0; i < cycles; i++) {
  115.         fn = rand() % NUMFILES; /* first, choose file we will deal with
  116.                                  * in this series */
  117.         if( fhs[fn] == 0) {     /* open if it is closed */
  118.             sprintf(name,"%sfile%04d.tst",dirname,fn);
  119.             if((fhs[fn]=open(name,O_RDWR|O_CREAT|O_TRUNC|O_TEXT,S_IWRITE|S_IREAD))==-1) {
  120.                 perror(name);
  121.                 exit(1);
  122.             }
  123.             printf("File #%d opened.\n", fn);
  124.             nowlen[fn] = 0;
  125.             nowpos[fn] = 0;
  126.         }
  127.  
  128.         rn = rand();
  129.         j = rn % 23;        /* j is afunction number - what will we do to
  130.                              * the file */
  131.         switch(j) {
  132. /*          case 0: */
  133.           case 1: {         /* kill the file  */
  134.             sprintf(name,"%sfile%04d.tst",dirname,fn);
  135.             close(fhs[fn]);
  136.             fhs[fn]=0;
  137.             remove(name);
  138.             printf("File #%d deleted.\n", fn);
  139.             break;
  140.           }
  141.           case 2: {         /* seek backwards or to the beginning of file */
  142.             seek_shift = (long)(rn % repetition)*47;
  143.             if(seek_shift < nowpos[fn])
  144.               nowpos[fn] = lseek(fhs[fn], -seek_shift, SEEK_CUR);
  145.             else
  146.               nowpos[fn] = lseek(fhs[fn], 0L, SEEK_SET);
  147.             break;
  148.           }
  149.           case 3: {         /* seek forward */
  150.             seek_shift = (long)(rn % repetition)*53;
  151.             if( seek_shift + nowpos[fn] < nowlen[fn])
  152.               nowpos[fn] = lseek(fhs[fn], seek_shift, SEEK_CUR);
  153.             else
  154.               nowpos[fn] = lseek(fhs[fn], 0L, SEEK_END);
  155.             break;
  156.           }
  157.           case 4: {         /* seek absolute from beginning of file */
  158.             seek_shift = (long)(rn % repetition)*41;
  159.             if( seek_shift < nowlen[fn])
  160.               nowpos[fn] = lseek(fhs[fn], seek_shift, SEEK_SET);
  161.             else
  162.               nowpos[fn] = lseek(fhs[fn], 0L, SEEK_END);
  163.             break;
  164.           }
  165.           case 5: {         /* seek absolute from end of file */
  166.             seek_shift = (long)(rn % repetition)*37;
  167.             if( seek_shift < nowlen[fn])
  168.               nowpos[fn] = lseek(fhs[fn], -seek_shift, SEEK_END);
  169.             else
  170.               nowpos[fn] = lseek(fhs[fn], 0L, SEEK_SET);
  171.             break;
  172.           }
  173.           case 6: {         /* run DOS command  */
  174.             system(command);
  175.             break;
  176.           }
  177.           default: {        /* for all the others just write random number
  178.                              * of lines into file from current position */
  179.             sprintf(text1,form1,fn,i);
  180.             len=strlen(text1);
  181.             rn = rn % repetition;
  182.             for(j=0; j < rn; j++)
  183.                 write(fhs[fn],text1,len);
  184.             nowpos[fn] += rn*len;
  185.             if(nowpos[fn] > nowlen[fn])
  186.                nowlen[fn] = nowpos[fn];
  187.           }
  188.         }
  189.       if(errno) {
  190.         fprintf(stderr,"File number %d :", fn);
  191.         perror(NULL);
  192.         exit(1);
  193.       }
  194.     }
  195.  
  196.     printf("    Good bye!!!\n");  /* all done */
  197.     for (i = 0; i < NUMFILES; i++)
  198.         if(fhs[i])
  199.         close(fhs[i]);            /* close all files  */
  200.     return(0);
  201. }
  202.